package com.sos.controller;


import com.sos.model.vo.ResultVO;
import com.sos.service.UserLocationService;
import com.sos.util.UserContextUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/location")
@RequiredArgsConstructor
@Tag(name = "位置服务", description = "用户位置相关接口")
@SecurityRequirement(name = "bearerAuth")
public class LocationController {

    private final UserLocationService userLocationService;
    private final UserContextUtils userContextUtils;

    /**
     * 更新当前用户位置
     */
    @PostMapping("/update")
    @Operation(summary = "更新用户位置", description = "更新当前用户的实时位置信息")
    public ResultVO<?> updateLocation(@RequestParam BigDecimal latitude,
                                      @RequestParam BigDecimal longitude) {
        Long userId = userContextUtils.getCurrentUserId();
        userLocationService.updateUserLocation(userId, latitude, longitude);
        return ResultVO.success("位置更新成功");
    }

    /**
     * 获取指定用户位置
     */
    @GetMapping("/{userId}")
    @Operation(summary = "获取用户位置", description = "获取指定用户的实时位置信息")
    public ResultVO<Map<Object, Object>> getUserLocation(@PathVariable Long userId) {
        Map<Object, Object> location = userLocationService.getUserLocation(userId);
        return ResultVO.success(location);
    }

    /**
     * 批量获取用户位置
     */
    @PostMapping("/batch")
    @Operation(summary = "批量获取用户位置", description = "批量获取多个用户的实时位置信息")
    public ResultVO<Map<Long, Map<Object, Object>>> getBatchUserLocations(@RequestBody List<Long> userIds) {
        Map<Long, Map<Object, Object>> locations = userLocationService.getBatchUserLocations(userIds);
        return ResultVO.success(locations);
    }
}